package GUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.SystemColor;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import Classes.Login;
import Repositorio.Rep_Util;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaLogin {

	private JFrame frmLoginDeAcesso;
	private JTextField txtEmail;
	private JPasswordField tpassSenha;

	/**
	 * Launch the application.
	 * @throws UnsupportedLookAndFeelException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaLogin window = new TelaLogin();
					window.frmLoginDeAcesso.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		
		//catch{ //(ClassNotFoundException | InstantiationException
			//	| IllegalAccessException | UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
	
	}
		finally
		{
			
		}
		
		
	}

	/**
	 * Create the application.
	 */
	public TelaLogin() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmLoginDeAcesso = new JFrame();
		frmLoginDeAcesso.setTitle("Login de Acesso");
		frmLoginDeAcesso.setAutoRequestFocus(false);
		frmLoginDeAcesso.setBounds(100, 100, 296, 188);
		frmLoginDeAcesso.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(176, 196, 222));
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 1, true));
		panel.setForeground(SystemColor.activeCaption);
		frmLoginDeAcesso.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JButton btnLogin = new JButton("Login");
		
		btnLogin.setBounds(9, 121, 89, 23);
		panel.add(btnLogin);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(185, 121, 89, 23);
		panel.add(btnCancelar);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(176, 196, 222));
		panel_1.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(9, 11, 265, 104);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel label = new JLabel("E-mail:");
		label.setBounds(10, 15, 68, 14);
		panel_1.add(label);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(88, 11, 155, 20);
		txtEmail.setColumns(10);
		panel_1.add(txtEmail);
		
		tpassSenha = new JPasswordField();
		tpassSenha.setBounds(88, 39, 155, 20);
		panel_1.add(tpassSenha);
		
		JLabel label_1 = new JLabel("Senha:");
		label_1.setBounds(10, 43, 68, 14);
		panel_1.add(label_1);
		
		JLabel lblTipoAcesso = new JLabel("Tipo Acesso:");
		lblTipoAcesso.setBounds(10, 70, 68, 14);
		panel_1.add(lblTipoAcesso);
		
		final JComboBox cmbTipoAcesso = new JComboBox();
		cmbTipoAcesso.setModel(new DefaultComboBoxModel(new String[] {"Professor", "Secret\u00E1rio"}));
		cmbTipoAcesso.setBounds(88, 68, 155, 20);
		panel_1.add(cmbTipoAcesso);
		
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				String nome = "";
				Rep_Util rep = new Rep_Util();
				Login login = new Login();
			
				
				if (cmbTipoAcesso.getSelectedIndex() == 0)
				{
					nome = rep.AcessoProfessor(txtEmail.getText(), tpassSenha.getText());
					if (nome.toString() != "")
					{
						login.setNome(nome);
						login.setTipo_acesso("P");
						JOptionPane.showMessageDialog(null, "Login Ok - Professor: " +login.getNome());
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Login erro" +login.getNome());
					}
				
				}
				if (cmbTipoAcesso.getSelectedIndex() == 1)
				{
					nome = rep.AcessoSecretario(txtEmail.getText(), tpassSenha.getText());
					if (nome.toString() != "")
					{
						login.setNome(nome);
						login.setTipo_acesso("S");
						JOptionPane.showMessageDialog(null, "Login Ok - Secretario: " +login.getNome());
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Login erro" +login.getNome());
					}
				}
				if (cmbTipoAcesso.getSelectedIndex() == 2)
				{
					
				}
			}		
				
			
		
		});
		
		
	}
}
